package com.mate.cloud.auth.config;

import com.mate.cloud.auth.service.impl.CustomUserDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

/**
 * 授权服务AuthorizationServerConfig配置
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createTime: 2024/6/14 18:11
 * @updateUser: MI
 * @updateTime: 2024/6/14 18:11
 * @updateRemark: 修改内容
 * @version: v1.0
 */
@Slf4j
@Configuration
public class AuthorizationServerConfig {

    // 配置自定义的 UserDetailsService
    @Bean
    public UserDetailsService userDetailsService() {
        return new CustomUserDetailsService();
    }


    //滤器链的相关设置
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        // 授权请求配置（新语法）
        http.authorizeHttpRequests(authorize -> authorize.anyRequest().authenticated())
                // 你还可以根据需求进行更多的配置，如自定义登录页面的 URL、登录成功或失败的处理逻辑等。
                .formLogin(e -> e.defaultSuccessUrl("/hello",true).permitAll());
        return http.build();
    }
    // 配置密码编码器
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
